package com.example.administrator.newfragmentation.fragment.simple;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.adpter.simple.PagerAdapter;
import com.example.administrator.newfragmentation.fragmentMain.BaseBlankFragment;
import com.example.administrator.newfragmentation.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PagerChildFragment extends BaseBlankFragment {

    private static final String ARG_FROM = "arg_from";
    @Bind(R.id.pager_childe_recycle)
    RecyclerView pagerChildeRecycle;
    private int index;

    private PagerAdapter mAdapter;

    public static PagerChildFragment newInstance(int index) {
        Bundle args = new Bundle();
        args.putInt(ARG_FROM, index);

        PagerChildFragment fragment = new PagerChildFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            index = bundle.getInt(ARG_FROM);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_pager_child, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter = new PagerAdapter(_mActivity);
        LinearLayoutManager manager = new LinearLayoutManager(_mActivity);
        pagerChildeRecycle.setLayoutManager(manager);
        pagerChildeRecycle.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                if (getParentFragment() instanceof DiscoverFragment) {
                    ((DiscoverFragment) getParentFragment()).start(CycleFragment.newInstance(1));
                }
            }
        });

        pagerChildeRecycle.post(new Runnable() {
            @Override
            public void run() {
                // Init Datas
                List<String> items = new ArrayList<>();
                for (int i = 0; i < 20; i++) {
                    String item;
                    if (index == 0) {
                        item = "推荐 " + i;
                    } else if (index == 1) {
                        item = "热门 " + i;
                    } else {
                        item = "收藏 " + i;
                    }
                    items.add(item);
                }
                mAdapter.setDatas(items);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        pagerChildeRecycle.setAdapter(null);
        ButterKnife.unbind(this);
    }
}
